package com.platform.usercenter.service.impl;

import com.platform.common.core.exception.ServiceException;
import com.platform.common.core.utils.DateUtils;
import com.platform.common.security.utils.SecurityUtils;
import com.platform.usercenter.domain.SysModelRoleFunction;
import com.platform.usercenter.mapper.SysModelRoleFunctionMapper;
import com.platform.usercenter.service.ISysModelRoleFunctionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色职能Service业务层处理
 *
 * @author wcs
 * @date 2025-09-21
 */
@Service
public class SysModelRoleFunctionServiceImpl implements ISysModelRoleFunctionService {
    @Resource
    private SysModelRoleFunctionMapper sysModelRoleFunctionMapper;

    /**
     * 查询角色职能
     *
     * @param id 角色职能主键
     * @return 角色职能
     */
    @Override
    public SysModelRoleFunction selectSysModelRoleFunctionById(Long id) {
        return sysModelRoleFunctionMapper.selectSysModelRoleFunctionById(id);
    }

    @Override
    public SysModelRoleFunction selectSysModelRoleFunctionByUserId(Long userId) {
        SysModelRoleFunction query = new SysModelRoleFunction();
        query.setUserId(userId);
        List<SysModelRoleFunction> fun = sysModelRoleFunctionMapper.selectSysModelRoleFunctionList(query);
        if(fun.size() > 0){
            return fun.get(0);
        }else {
            return null;
        }
    }

    /**
     * 查询角色职能列表
     *
     * @param sysModelRoleFunction 角色职能
     * @return 角色职能
     */
    @Override
    public List<SysModelRoleFunction> selectSysModelRoleFunctionList(SysModelRoleFunction sysModelRoleFunction) {
        return sysModelRoleFunctionMapper.selectSysModelRoleFunctionList(sysModelRoleFunction);
    }

    /**
     * 新增角色职能
     *
     * @param sysModelRoleFunction 角色职能
     * @return 结果
     */
    @Override
    public int insertSysModelRoleFunction(SysModelRoleFunction sysModelRoleFunction) {
        sysModelRoleFunction.setCreateTime(DateUtils.getNowDate());
        sysModelRoleFunction.setCreateBy(SecurityUtils.getUsername());
        sysModelRoleFunction.setIsDelete("0");
        SysModelRoleFunction query = new SysModelRoleFunction();
        query.setUserId(sysModelRoleFunction.getUserId());
        List<SysModelRoleFunction> functions = sysModelRoleFunctionMapper.selectSysModelRoleFunctionList(query);
        if (functions.size() > 0) {
            throw new ServiceException("该用户已存在角色职能，目前一个用户只支持设置一个角色职能");
        }
        return sysModelRoleFunctionMapper.insertSysModelRoleFunction(sysModelRoleFunction);
    }

    /**
     * 修改角色职能
     *
     * @param sysModelRoleFunction 角色职能
     * @return 结果
     */
    @Override
    public int updateSysModelRoleFunction(SysModelRoleFunction sysModelRoleFunction) {
        SysModelRoleFunction query = new SysModelRoleFunction();
        query.setUserId(sysModelRoleFunction.getUserId());
        List<SysModelRoleFunction> functions = sysModelRoleFunctionMapper.selectSysModelRoleFunctionList(query);
        functions = functions.stream().filter(function -> !function.getId().equals(sysModelRoleFunction.getId())).collect(Collectors.toList());
        if (functions.size() > 0) {
            throw new ServiceException("该用户已存在角色职能，目前一个用户只支持设置一个角色职能");
        }
        sysModelRoleFunction.setUpdateTime(DateUtils.getNowDate());
        sysModelRoleFunction.setUpdateBy(SecurityUtils.getUsername());
        return sysModelRoleFunctionMapper.updateSysModelRoleFunction(sysModelRoleFunction);
    }

    /**
     * 批量删除角色职能
     *
     * @param ids 需要删除的角色职能主键
     * @return 结果
     */
    @Override
    public int deleteSysModelRoleFunctionByIds(Long[] ids) {
        return sysModelRoleFunctionMapper.deleteSysModelRoleFunctionByIds(ids);
    }

    /**
     * 删除角色职能信息
     *
     * @param id 角色职能主键
     * @return 结果
     */
    @Override
    public int deleteSysModelRoleFunctionById(Long id) {
        return sysModelRoleFunctionMapper.deleteSysModelRoleFunctionById(id);
    }
}
